package com.wyp.didishige.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.wyp.didishige.activity.im.ChatActivity;
import com.wyp.didishige.adapter.MessageRecentAdapter;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.wiget.ClearEditText;
import com.wyp.didishige.wiget.dialog.DialogTips;
import com.wyp.didishige.R;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;

/**
 * 最近会话
 *
 * @author smile
 * @ClassName: ConversationFragment
 * @Description: TODO
 * @date 2014-6-7 下午1:01:37
 */
@SuppressWarnings("ALL")
public class RecentFragment extends FragmentBase implements OnItemClickListener, OnItemLongClickListener {

    ClearEditText mClearEditText;

    ListView listview;
    private static final String ARG_SECTION_NUMBER = "section_number";

    public static RecentFragment newInstance(int sectionNumber) {
        RecentFragment fragment = new RecentFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    MessageRecentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recent, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {

        listview = (ListView) findViewById(R.id.bmobIMMessageList);
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);
        adapter = new MessageRecentAdapter(getActivity(), R.layout.item_conversation, BmobIM.getInstance().loadAllConversation());
        listview.setAdapter(adapter);

        mClearEditText = (ClearEditText) findViewById(R.id.et_msg_search);
        mClearEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                adapter.getFilter().filter(s);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }

    /**
     * 删除会话
     * deleteRecent
     *
     * @param @param recent
     * @return void
     * @throws
     */
    private void deleteRecent(BmobIMConversation recent) {
        adapter.remove(recent);
        BmobIM.getInstance().deleteConversation(recent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,
                                   long arg3) {
        BmobIMConversation recent = adapter.getItem(position);
        showDeleteDialog(recent);
        return true;
    }

    public void showDeleteDialog(final BmobIMConversation recent) {
        DialogTips dialog = new DialogTips(getActivity(), recent.getConversationTitle(), "删除会话", "确定", true, true);
        // 设置成功事件
        dialog.SetOnSuccessListener(new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int userId) {
                deleteRecent(recent);
            }
        });
        // 显示确认对话框
        dialog.show();
        dialog = null;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        BmobIMConversation recent = adapter.getItem(position);
        //重置未读消息
        recent.updateLocalCache();
        //组装聊天对象
        Bundle bundle=new Bundle();
        bundle.putSerializable(Config.KEY_CONVERSATION,recent);
        Intent intent = new Intent(getActivity(), ChatActivity.class);
        intent.putExtras(bundle);
        startAnimActivity(intent);
        adapter.notifyDataSetChanged();
    }

    public void refresh() {
        try {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    adapter = new MessageRecentAdapter(getActivity(), R.layout.item_conversation, BmobIM.getInstance().loadAllConversation());
                    listview.setAdapter(adapter);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

}
